<?php
defined('IN_TS') or die('Access Denied.');

class historyAction extends history{

    /*
     * 首页
     */
    public function index(){

        $year = isset($_GET['year']) ? intval($_GET['year']) : date('Y');
		$cateid = $_GET['cateid'];
		$arrCate = $this->findAll('history_cate','','orderid asc');

		for($i=1;$i<=12;$i++){
			if(in_array($i,array(1,3,5,7,8,10,12))){
				$x=31;
			}else{
				if(in_array($i,array(4,6,9,11))){
					$x=30;
				}else{
					if(($year%100!=0&&$year%4==0)||$year%400==0){
						$x=29;
					}else{
						$x=28;
					}
				}
			}
			$begindate = strtotime($year.'-'.$i.'-1 0:00');
			$enddate = strtotime($year.'-'.$i.'-'.$x.' 24:00');
			$str = 'begindate>'.$begindate.' and begindate<'.$enddate;
			if($cateid){
				$str = $str.' and cateid='.$cateid;
			}
			$strHistory[$i] = $this->findAll('history',$str,'begindate desc');
			if($strHistory[$i]){
				$arrHistory[$i]=$strHistory[$i];
				foreach($arrHistory[$i] as $key=>$item){
					$arrHistory[$i][$key]['content'] = tsDecode($item['content']);
				}

			}
		}


        $title = '时间轴';
        include template('index');
    }

    /*
     * 发布动态
     */
    public function add(){

        $js = intval($_GET['js']);
        $userid = aac('user')->isLogin(1);

        //判断发布者状态
		if ($userid == 0) {
			getJson('请登录后再操作', $js);
		}
		if($GLOBALS['TS_USER']['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		
        if(aac('user')->isPublisher()==false) {
            getJson('不好意思，你还没有权限发布内容！',$js);
        }


		$begindate = $_POST['begindate'];
		$grade = $_POST['grade'];
		$title = trim($_POST['title']);
        $content = tsClean($_POST['content']);
		$cateid = $_POST['cateid'];

		if($begindate == '') {
			getJson('日期不能为空',$js);
		}
		if($cateid == '') {
			getJson('类别不能为空',$js);
		}
		if($title == '') {
			getJson('标题不能为空',$js);
		}
        if($content == '') {
            getJson('详情不能为空',$js);
        }

        $begindate = strtotime($begindate);

        //过滤内容开始
        aac('system')->antiWord($content);
        //过滤内容结束


        $historyid = $this->create('history',array(
            'userid'=>$userid,
			'cateid'=>$cateid,
            'grade'=>$grade,
			'begindate'=>$begindate,
            'content'=>$content,
            'title'=>$title,
            'addtime'=>time()
        ));

		$year = date('Y',$begindate);

        getJson('发布成功！',$js,2,tsurl('history','index',array('year'=>$year)));
		
    }

	/*
	 *展示动态内容
	 */
    public function show(){
        $historyid = intval($_GET['id']);

        $strHistory = $this->getOneHistory($historyid);

        if($historyid==0 || $strHistory==''){
            ts404();
        }

        if($strHistory['isaudit']==1){
            tsNotice('内容审核中...');
        }

        //comment
        $page = isset($_GET['page']) ? intval($_GET['page']) : '1';
        $url = tsUrl('history','show',array('id'=>$historyid,'page'=>''));
        $lstart = $page*20-20;

        $arrComments = $this->findAll('history_comment',array(
            'historyid'=>$historyid,
        ),'addtime desc','commentid',$lstart.',20');

        foreach($arrComments as $key=>$item){
            $arrComment[] = $this->getOneComment($item['commentid']);
        }

        $commentNum = $this->findCount('history_comment',array(
            'historyid'=>$historyid,
        ));

        $pageUrl = pagination($commentNum, 20, $page, $url);



        //他的更多动态
        $arrHistory = $this->findAll('history',array(
            'userid'=>$strHistory['userid'],
        ),'addtime desc',null,20);

        $historyNum = $this->findCount('history',array(
            'userid'=>$strHistory['userid'],
        ));

        if($historyNum<20){

            $num = 20-$historyNum;
            $userid = $strHistory['userid'];
            $arrNewHistory = $this->findAll('history',"`userid`!='$userid'",'addtime desc',null,$num);

            $arrHistory = array_merge($arrHistory, $arrNewHistory);

        }



        $title = cututf8(t(tsDecode($strHistory['content'])),0,100,false);

        include template('show');
    }
	
	
	/*
	 * 发布动态图片
	 */
	public function photo(){

		$userid = intval($GLOBALS['TS_USER']['userid']);

		if($userid==0){
			echo 0;exit;//请登录
		}


		$content = tsClean($_POST['content']);

		if($GLOBALS['TS_USER']['isadmin']==0){
			//过滤内容开始
			aac('system')->antiWord($content);
			//过滤内容结束
		}

		$historyid = $this->create('history',array(
			'userid'=>$userid,
			'content'=>$content,
			'isaudit'=>0,
			'addtime'=>date('Y-m-d H:i:s'),
			'uptime'=>date('Y-m-d H:i:s'),
		));

		// 上传图片开始
		$arrUpload = tsUpload ( $_FILES ['filedata'], $historyid, 'history', array ('jpg','gif','png','jpeg' ) );
		if ($arrUpload) {
			$this->update ( 'history', array (
					'historyid' => $historyid
			), array (
					'path' => $arrUpload ['path'],
					'photo' => $arrUpload ['url'] 
			) );
			
			echo 3;exit;
			
		}else{
			
			echo 2;exit;

		}
	}
	
	/*
	 * 回复动态，添加评论
	 */
	public function addcomment(){

		//用户是否登录
		$userid = aac('user')->isLogin();
		$historyid = intval($_POST['historyid']);
		$touserid = intval($_POST['touserid']);
		$content = tsClean($_POST['content']);

		if($content == ''){
			tsNotice('内容不能为空');
		}

		if($GLOBALS['TS_USER']['isadmin']==0){
			//过滤内容开始
			aac('system')->antiWord($content);
			//过滤内容结束
		}

		$commentid = $this->create('history_comment',array(
			'userid'=>$userid,
			'touserid'=>$touserid,
			'historyid'=>$historyid,
			'content'=>$content,
			'addtime'=>date('Y-m-d H:i:s'),
		));

		//计算评论总数
		$commentNum = $this->findCount('history_comment',array(
			'historyid'=>$historyid,
		));

		$this->update('history',array(
			'historyid'=>$historyid,
		),array(
			'count_comment'=>$commentNum,
		));
		
		$strHistory = $this->find('history',array(
			'historyid'=>$historyid,
		));
		
		if($strHistory['userid'] != $userid){
			$msg_userid = '0';
			$msg_touserid = $strHistory['userid'];
			$msg_content = '你的微博新增一条回复，快去看看给个回复吧^_^';
            $msg_tourl = tsUrl('history','show',array('id'=>$historyid));
			aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);
		}

		tsHeaderUrl(tsUrl('history','show',array('id'=>$historyid)));
	}
	
	/*
	 * 删除评论
	 */
	public function deletecomment(){
		$userid = aac('user')->isLogin();
	
		$commentid = intval($_GET['commentid']);
		
		$strComment = $this->find('history_comment',array(
			'commentid'=>$commentid,
		));
	
		if($GLOBALS['TS_USER']['isadmin']==1 || $strComment['userid']==$userid){
			
			
			$this->delete('history_comment',array('commentid'=>$commentid));
			
			//统计
			$count_comment = $this->findCount('history_comment',array(
				'historyid'=>$strComment['historyid'],
			));
			
			$this->update('history',array(
				'historyid'=>$strComment['historyid'],
			),array(
				'count_comment'=>$count_comment,
			));
			
			tsHeaderUrl(tsUrl('history','show',array('id'=>$strComment['historyid'])));
			
			
		}else{
			tsNotice('非法操作！');
		}
	}
	
	/*
	 * 删除动态
	 */
	public function deletehistory(){
		$userid = aac('user')->isLogin();
		//判断发布者状态
		if ($userid == 0) {
			getJson('请登录后再操作', $js);
		}

		$historyid = intval($_GET['historyid']);

		$strHistory = $this->find('history',array(
			'historyid'=>$historyid,
		));

		if($userid == $strHistory['userid'] || $GLOBALS['TS_USER']['isadmin']==1){
			$this->delete('history',array(
				'historyid'=>$historyid,
			));

			echo 1;
			tsHeaderUrl(tsUrl('history'));
			
		}else{
			tsNotice('非法操作！');
		}
	}

    /*
     * 后台管理入口
     * */
    public function admin(){

        if($GLOBALS['TS_USER']['isadmin']==1){
            include 'app/'.$GLOBALS['TS_URL']['app'].'/admin.'.$GLOBALS['TS_URL']['app'].'.php';
            $appAdmin = $GLOBALS['TS_URL']['app'].'Admin';
            $newAdmin = new $appAdmin($GLOBALS['db']);
            #$newAdmin->$GLOBALS['TS_URL']['mg']();

            $amg = $GLOBALS['TS_URL']['mg'];
            $newAdmin->$amg();

        }else{
            ts404();
        }
    }

    /*
     * 我的社区入口
     * */
    public function my(){
        if($GLOBALS['TS_USER']){
            include 'app/'.$GLOBALS['TS_URL']['app'].'/my.'.$GLOBALS['TS_URL']['app'].'.php';
            $appMy = $GLOBALS['TS_URL']['app'].'My';
            $newMy = new $appMy($GLOBALS['db']);
            $newMy->$GLOBALS['TS_URL']['my']();
        }else{
            ts404();
        }
    }

}